package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;

import com.joplus.housekeeper.utils.L;
import com.joplus.housekeeper.utils.StringUtils;


import org.json.JSONObject;

public class EaseChatRowText_ReWard extends EaseChatRow {

    private TextView contentView;

    private TextView tv_result;
    String rewardId;


    public EaseChatRowText_ReWard(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_reward: R.layout.ease_row_sent_message, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        //card



        tv_result=(TextView)findViewById(R.id.tv_result);
        tv_result.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rewardId==null){
                    return;
                }

            }
        });

    }

    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);
        String msg = txtBody.getMessage();
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            bubbleLayout.setVisibility(View.GONE);
            contentView.setVisibility(View.GONE);
            String subject = chatCard.getSubject();
            if (subject.equals("reward")) {
                rewardId=chatCard.getRewardId();
                setTip(chatCard);
            }
        } catch (Exception e) {
            L.e("onSetUpView()", "position-->" + position + "--error-->" + e.toString());
            bubbleLayout.setVisibility(View.VISIBLE);
//            layoutTip.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);


        }
//        handleTextMessage();

    }


    private void setTip(final ChatCard chatCard) {

//        tv_result.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, EvaluateActivity.class);
//                context.startActivity(intent);
//            }
//        });

    }


    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }


}
